package Options::Attributes::OBJ::BasketOperations;

use base qw (Exporter);
use strict;
use warnings;

use DE_EPAGES::Core::API::Log qw (LogDebug GetLog);
use DE_EPAGES::WebInterface::API::ApplicationServer qw (GetAppServer);
use DE_EPAGES::Presentation::API::Session
    qw( GetSessionAttribute SetSessionAttribute );
use DE_EPAGES::Customer::API::Shop qw (GetCustomerForUser);
use DE_EPAGES::Order::API::LineItemContainer qw(SavePayment SaveShipping);
use DE_EPAGES::Object::API::Factory
    qw( LoadObject ExistsObjectByPath LoadObjectByPath LoadClassByAlias);
use DE_EPAGES::Core::API::Error qw( Error ExistsError GetError );
use DE_EPAGES::WebInterface::API::ApplicationServer qw (GetAppServer);
use Data::Dumper;

#IF this is the last product in the basket delete LineItemProductUpdate
# what would also delete the basket itself
sub DeleteLineItemProduct {
    my ($hParams) = @_;
    GetLog->debug("HOOK DeleteLineItemProduct - START");
    my $LineItemProduct   = $hParams->{Object};
    my $LineItemContainer = $LineItemProduct->get('Parent');
    my $LineItemProducts
        = $LineItemContainer->childrenByClass('LineItemProduct');
    if ( ( scalar @$LineItemProducts ) <= 1 ) {
        my $LineItemProductUpdate
            = $LineItemContainer->childrenByClass('LineItemProductUpdate');
        if ( ( scalar @$LineItemProductUpdate ) > 0 ) {
            foreach my $LineItemProductUpdateItem (@$LineItemProductUpdate) {
                $LineItemProductUpdateItem->delete;
            }
        }
    }
    GetLog->debug(
        "HOOK DeleteLineItemProduct: " . scalar @$LineItemProducts );
    return;
}

1;
